import { McpServer, ResourceTemplate } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { z } from "zod";
// import { Anthropic } from "@ai-sdk/openai";
console.log("Server is running");

// Create an MCP server
const server = new McpServer({
  name: "Demo",
  version: "1.0.0",
  tools: [
    {
      name: "add",
      description: "Add two numbers",
      parameters: z.object({ a: z.number(), b: z.number() })
    },
    {
      name: "weather",
      description: "Get the weather of a cityCode",
      parameters: z.object({ cityCode: z.string() })
    }
  ]
});

// Add an addition tool
server.tool("add",
  { a: z.number(), b: z.number() },
  async ({ a, b }) => ({
    content: [{ type: "text", text: String(a + b) }]
  })
);

// 查询天气得工具
server.tool("weather",
  { cityName: z.string() },

  async ({ cityName }) => {
    const name = !cityName.includes("市") ? cityName + "市" : cityName;
    const response = await fetch(`https://uapis.cn/api/weather?name=${name}`);
    const data = await response.json();
    // 输出完整的天气信息包括温度
    return {
      content: [{
        type: "text",
        text: `天气信息 - ${cityName}:
        天气: ${data.weather}
        温度: ${data.temperature}°C
        湿度: ${data.humidity}
        风向: ${data.wind_direction}
        风力: ${data.wind_power}
        更新时间: ${data.reporttime}`
      }]
    };
  }
);

// Add a dynamic greeting resource
server.resource(
  "greeting",
  new ResourceTemplate("greeting://{name}", { list: undefined }),
  async (uri, { name }) => ({
    contents: [{
      uri: uri.href,
      text: `Hello, ${name}!`
    }]
  })
);

// Static resource
server.resource(
  "config",
  "config://app",
  async (uri) => ({
    contents: [{
      uri: uri.href,
      text: "App configuration here"
    }]
  })
);

// Start receiving messages on stdin and sending messages on stdout
const transport = new StdioServerTransport();
await server.connect(transport);